﻿using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;

namespace TraceThisItem.Domain.Initializers
{
	public class ProjectConfiguration : EntityTypeConfiguration<Project>
	{
		public ProjectConfiguration()
		{
			// Define la PK
			HasKey(project => project.Id)
				.Property(project => project.Id)
				.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

			Property(project => project.Name)
				.HasMaxLength(50)	// varchar(50)
				.IsRequired()		// Para que sea NOT NULL
				.IsUnicode(false);	// Para que sea varchar (1 byte por char) en lugar de nvarchar (2 bytes por char).

			HasMany(project => project.Teams)
				.WithRequired(team => team.Project)
				.WillCascadeOnDelete(false);

			HasOptional(project => project.Owner)
				.WithMany(user => user.OwnedProjects)
				.HasForeignKey(project => project.OwnerUserName);

			HasMany(project => project.ItemTypes)
				.WithRequired(it => it.Project)
				.WillCascadeOnDelete();
		}
	}
}